var prefix = top.ctx + "/animal/warningSet"
$(function () {
    template.defaults.imports.random = function () {
        return Math.floor(Math.random() * 100000000);
    };
    setAllFormData();
});

//后端获取数据  赋到表单中
function setAllFormData() {
    // var data = [warningSetNullObject(),warningSetNullObject()];
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/listAll",
        async: false,
        error: function (request) {
            layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                var data = data.data;
                var html = template("formTemplate", {list: data});
                $('#formContent').html(html);

                $("input[name='iRandom']").each(function (i) {
                    var iRandom = $(this).val();
                    $('#animalType' + iRandom).val(data[i].animalType);
                    $('#animalVarieties' + iRandom).val(data[i].animalVarieties);
                    $('#dialogTipSwitch' + iRandom).val(data[i].dialogTipSwitch);
                    // $('#warningSet_station_dialogTipSwitch' + iRandom).val(data[i].warningSet.station.dialogTipSwitch);
                    // $('#warningSet_tag_dialogTipSwitch' + iRandom).val(data[i].warningSet.tag.dialogTipSwitch);
                });
                // for (let i = 0; i < data.length; i++) {
                //     $('#animalType' + i).val(data[i].animalType);
                //     $('#animalVarieties' + i).val(data[i].animalVarieties);
                // }
            } else {
                layer.alert(data.msg)
            }

        }
    });
}

//获取所有的表单数据
function getAllFormData() {
    var warningSets = new Array();
    $(".form-horizontal").each(function (i) {
        warningSets[i] = warningSetNullObject();
        var array = $(this).serializeArray();
        $.each(array, function (j, v) {
            eval("warningSets[i]." + v.name + "= v.value");
        })
    });
    console.log(warningSets);
    return warningSets;
}

//创建一个控对象
function warningSetNullObject() {
    return {
        "animalType": null,
        "animalVarieties": null,
        "dialogTipSwitch": null,
        "warningSet": {
            "oestrus": {
                "endBirthday": null,
                "startBirthday": null
            },
            "parturition": {
                "endHybridizationDay": null,
                "startHybridizationDay": null,
                "upperBodyTemperature": null
            },
            "sick": {
                "lowerBodyTemperature": null,
                "upperBodyTemperature": null
            },
            "station": {
                "daGpsDuration": null,
                "daGpsTimes": null,
                "daPowerDuration": null,
                "daPowerTimes": null,
                "daElecDuration": null,
                "daElecTimes": null,
                "daElecMin": null,
                "upLoadDuration": null,
                "dialogTipSwitch": null
            },
            "tag": {
                "daElecDuration": null,
                "daElecTimes": null,
                "daElecMin": null,
                "upLoadDuration": null,
                "dialogTipSwitch": null
            }
        }
    }
}

//添加一个表单
function addForm() {
    var data = [warningSetNullObject()];
    var html = template("formTemplate", {list: data, renderType: 'add'});
    //分割线
    $('.form-horizontal').last().append("<h5 class=\"page-header\"></h5>")
    $('.form-horizontal-div').last().after(html);
    ;
}

//移除一个表单
function removeForm(obj) {
    if ($('.form-horizontal-div').length == 1) {
        layer.alert('必须保留一个预警配置！')
        return;
    }
    $(obj).parents('.form-horizontal-div').remove();
}

function save() {
    var formDataArray = getAllFormData();
    $.ajax({
        cache: true,
        type: "POST",
        contentType: 'application/json',
        url: prefix + "/save",
        data: JSON.stringify(formDataArray),//
        async: false,
        error: function (request) {
            layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                layer.msg("操作成功");
            } else {
                layer.alert(data.msg)
            }

        }
    });
}
